/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package page;

import dbase.DBconnection;
import dbase.DBkendaraan;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import model.*;
/**
 *
 * @author aa
 */
public class GoKendaraan {
    HttpServletRequest request;

    public GoKendaraan(HttpServletRequest request){
        this.request = request;
    }


    public String addKendaraan(){
        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null) {
            String noPolisi = request.getParameter("noPol");
            String noRangka = request.getParameter("noRang");
            String noSTNK = request.getParameter("noStnk");
            String noMesin = request.getParameter("noMes");
            String warna = request.getParameter("warna");
            String merek = request.getParameter("merek");
            String jenisMotor = request.getParameter("jenis");
            String nrp = request.getParameter("nrp");
            DBconnection dbConn = new DBconnection();
            DBkendaraan dbRegister = new DBkendaraan(dbConn.getConnection());
            try{
                dbRegister.insertKendaraan(noPolisi, noRangka, noMesin, noSTNK, warna, merek, jenisMotor, nrp);
            }catch(Exception ex){
                ex.printStackTrace();
            }finally{
                dbConn.closeConnection();
            }
            return "index.jsp";
        }
        return "index.jsp";
    }

    public String updateKendaraan(){
        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null) {
            String noPolisi = request.getParameter("noPol");
            String noRangka = request.getParameter("noRang");
            String noMesin = request.getParameter("noMesin");
            String noSTNK = request.getParameter("noSTNK");
            String warna = request.getParameter("warna");
            String merek = request.getParameter("merek");
            String jenisMotor = request.getParameter("jenis");
            String nrp = request.getParameter("nrp");
            kendaraan kendaraan1 = new kendaraan();
            DBconnection dbConn = new DBconnection();
            DBkendaraan mdEdit = new DBkendaraan(dbConn.getConnection());

             try{
                    mdEdit.updateKendaraan(noPolisi, noRangka, noMesin, noSTNK, warna, merek, jenisMotor, nrp);
            }catch(Exception ex){
                ex.printStackTrace();
            }finally{
                dbConn.closeConnection();
                return getPage2();
            }

    }
        else {
            return "index.jsp";
        }
    }

public String getPage2(){
        //String catid = request.getParameter("catid");
        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null) {
            DBconnection dbConn = new DBconnection();
            List<kendaraan> kend = new ArrayList<kendaraan>();
            DBkendaraan dbKend = new DBkendaraan(dbConn.getConnection());

            try{
                kend = dbKend.getAllKendaraan();
            }catch(Exception ex){
                ex.printStackTrace();
            }finally{
                dbConn.closeConnection();
            }
            request.setAttribute("datakendaraan", kend);
            return "lihatKendaraanAll.jsp";
    }
        else{
            return "index.jsp";
        }
    }

}
